#!/bin/bash
# DJVU_Page_Range_1.sh ( 28 )
# This script takes four arguments , the input file , the output file , the starting page , and the ending page . It takes the specified page range from the input file and puts it into the specified output file .
File1="$1" ;
[ "x$TMPDIR" == x ] && TMPDIR=/tmp
tmpname="$TMPDIR/Electric_Document_$$.tmp"
mkdir -p "$tmpname"
Page_Count_1=`djvudump $1 | awk '{ count = 0 } /pages/ { for(i=2;i<=NF;i++){ if($i ~ "pages"){b=i-1; print $b}}}'` ;
File1="$1" ;
Output1="$2" ;
Start1="$3" ;
End1="$4" ;
if [ ! "$Start1" -ge 0 ] ;
then
	Start1=0 ;
fi ;
if [ ! "$Start1" -le "$Page_Count_1" ] ;
then
	Start1="$Page_Count_1" ;
fi ;
if [ ! "$End1" -ge 0 ] ;
then
        End1=0 ;
fi ;
if [ ! "$End1" -le "$Page_Count_1" ] ;
then
        End1="$Page_Count_1" ;
fi ;
if [ ! "$Start1" -le "$End1" ] ;
then
        End1="$Start1" ;
fi ;
Position="$Start1" ;
Count1=0 ;
while [ "$Position" -le "$End1" ] ;
do
	Count1=`expr $Count1 + 1` ;
	djvused "$File1" -e "select $Position ; save-page \"$tmpname/Page_`printf "%04d" $Count1`.C.djvu\" ;" ;
	Position=`expr $Position + 1` ;
done ;
djvm -c "$Output1" "$tmpname"/Page_????.C.djvu ;
rm -r "$tmpname" ;

